<?php

/**
 * Description of PacksController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Packs\Backend\Controller;

use Library\BackController;
use Library\HttpRequest;
use Library\Classe\Form\Form;

class PacksController extends BackController{
    
    public function executePacks(HttpRequest $request){
       $this->page->addVar('title', 'Packs');
       
       $manager = $this->managers->getManagerOf('Packs');
       
       $packs = $manager->getPacks();
        
        $this->page->addVar('packs', $packs);
        $this->leftcolumn();
    }
    
    private function leftcolumn(){
        $out = array();
        $out['packs.html']= 'Liste des Packs';
        $out['add-packs.html']= 'Ajouter un Packs';
        
        return $this->page->addVar('left_content', $out);
        
    }
    
    public function executeCreate(HttpRequest $request){
        $this->page->addVar('title', 'ajouter un Packs');
        $this->leftcolumn();
        $manager = $this->managers->getManagerOf('Packs');
        
        $packsForm = new Form('addPacks');
        $packsForm->action($request->requestURI());
        //ajout des champs
        $packsForm->add('Text', 'credits')
                ->label('Crédits')
                 ->required(true);
        
        $packsForm->add('Text', 'prix')
                ->label('Prix')
                ->required(true);
        
        if($request->getExists('id')){
            $packsForm->add('Hidden', 'id')->value($request->getData('id'));
            $this->page->addVar('title', 'Modifier un Packs');
            $packsForm->add('Submit', 'submit')->value('Modifier');
        }else{
            $packsForm->add('Submit', 'submit')->value('Ajouter');
        }
        if($request->getMethod('post')){
           //test de la validation du post
            if ($packsForm->is_valid($_POST)) {
                list($credits, $prix) = $packsForm->get_cleaned_data('credits','prix');
                $_POST['credits'] = intval($credits);
                $_POST['prix'] = intval($prix);
                if(!$request->getExists('id')){
                    if($manager->addPack($_POST)){
                        $this->app()->httpResponse()->redirect('packs.html');
                    }else{
                        $this->errors = 'Echec lors de l\'enregistrement';
                    }
                }else{
                    if($manager->updatePack($_POST)){
                        $this->app()->httpResponse()->redirect('packs.html');
                    }else{
                        $this->errors = 'Echec lors de la mise à jour';
                    }
                }
            }else{
                $packsForm->bound($_POST);
            }
        }else{
            if($request->getExists('id')){
               $pack = $manager->getPackById(intval($request->getData('id')));
               $faq_array['credits'] = $pack->getCredits();
               $faq_array['prix'] = $pack->getPrix();
               $packsForm->bound($faq_array);
            }else{
                $packsForm->bound($_POST);
            }
        }
        $this->page->addVar('errors', $this->errors);
        $this->page->addVar('packsForm', $packsForm);  
    }
    
    public function executeDelete(HttpRequest $request){
        
        $manager = $this->managers->getManagerOf('Packs');
        if($request->getExists('id')){
            $out['id'] = $request->getData('id');
            if($manager->delPack($out)){
                $this->app()->httpResponse()->redirect('packs.html');
            }else{
                $this->app()->httpResponse()->redirect('packs.html');
            }
        }
    }
}

?>
